<?php

class Registration_IndexController extends WSB_Xtension_Static
{

	private $_errors;

	public function saveAction()
	{

		$input = $this->_getFilterInput();
		$this->_errors = array();
		if($this->getRequest()->isPost()) {
		    if ($input->isValid()) {
		    	$data = $input->getUnescaped();
	    		$data['password'] = $data['password'];
			    $table = new Member_Model_DbTable_Feuser();
	    		$item = $table->createRow($data);
	    		$item->save();
	    		$this->addContent($this->_view->render('registered.phtml'));
		    } else {
				$this->_errors = $input->getMessages();
				$this->indexAction($input);
		    }
		}

	}

	public function indexAction($input = null)
	{

		if(is_null($input)) {
			$input = $this->_getFilterInput();
		}

    	$this->_view->errors = $this->_errors;
		$this->_view->input = $input;
		$this->_view->action = $this->getRequest()->getRequestUri();

		$this->addContent($this->_view->render('form.phtml'));

	}

	private function _getFilterInput()
	{
		// Validators
		$alnum = new Zend_Validate_Alnum();
		$alnum->setMessage($this->translate('alnum_string_empty'), Zend_Validate_Alnum::STRING_EMPTY);
		$alnum->setMessage($this->translate('alnum_not_alnum'), Zend_Validate_Alnum::NOT_ALNUM);

		$alnumWhitespace = new Zend_Validate_Alnum(true);
		$alnumWhitespace->setMessage($this->translate('alnum_string_empty'), Zend_Validate_Alnum::STRING_EMPTY);
		$alnumWhitespace->setMessage($this->translate('alnum_not_alnum'), Zend_Validate_Alnum::NOT_ALNUM);

		$stringLength = new Zend_Validate_StringLength(4, 50);
		$stringLength->setMessage($this->translate('stringlength_too_short'), Zend_Validate_StringLength::TOO_SHORT);
		$stringLength->setMessage($this->translate('stringlength_too_long'), Zend_Validate_StringLength::TOO_LONG);

		$uniqueUsername = new WSB_Validator_UniqueFeuser(WSB_Validator_UniqueFeuser::MODE_USER);
		$uniqueUsername->setMessage($this->translate('username_already_exists'), WSB_Validator_UniqueFeuser::USERNAME_EXISTS);

		$stringEquals = new WSB_Validator_StringEquals();
		$stringEquals->setMessage($this->translate('passwords_do_not_match'), WSB_Validator_StringEquals::NOT_EQUALS);

		/* Pattern von TYPO3s t3lib_div::validEmail() */
		$emailRegex = new Zend_Validate_Regex(WSB_Div::getEmailRegex());
		$emailRegex->setMessage($this->translate('email_invalid'), Zend_Validate_Regex::NOT_MATCH);

		/* Variante mit Zend_Filter_Input */
		$validators = array(
			'username' => array(
				$alnum,
				$stringLength,
				$uniqueUsername
			),
			'password' => array(
				$alnumWhitespace
			),
			'password_repeated' => array(
				$alnumWhitespace,
				$stringEquals,
				'fields' => array('password', 'password_repeated')
			),
			'firstname' => array(
				$alnumWhitespace
			),
			'lastname' => array(
				$alnumWhitespace
			),
			'email' => array(
				$emailRegex,
				'allowEmpty' => false
			)
		);

		// Filter
		$filters = array(
			'*' => 'StringTrim'
		);

		return new Zend_Filter_Input($filters, $validators, $this->getParams());

	}
}
